const app = getApp()
//微信同声传译
const plugin = requirePlugin('WechatSI');
Page({
    data: {
        src:"",
        title: '',
        image: '',
        description: '',
        filename:"",
    },
    onReady(e) {
        //创建内部 audio 上下文 InnerAudioContext 对象。
        this.innerAudioContext = wx.createInnerAudioContext();
        this.innerAudioContext.onError(function (res) {
          console.log(res);
          wx.showToast({
            title: '语音播放失败',
            icon: 'none',
          })
        }) 
      },
      change:function(){
        var that = this;
        that.robot()
      },
      // 文字转语音
      begin:function (e) {
        var that = this;
        var msg = this.data.description
        plugin.textToSpeech({
          lang: "zh_CN",
          tts: true,
          content: msg,
          success: function (res) {
            console.log(res);
            console.log("succ tts", res.filename);
            that.setData({
              src: res.filename
            })
            that.yuyinPlay();
     
          },
          fail: function (res) {
            console.log("fail tts", res)
          }
        })
      },
      
      //播放语音
      yuyinPlay: function (e) {
        if (this.data.src == '') {
          console.log(暂无语音);
          return;
        }
        this.innerAudioContext.src = this.data.src //设置音频地址
        this.innerAudioContext.play(); //播放音频
      },
     
      // 结束语音
      end: function (e) {
        this.innerAudioContext.pause();//暂停音频
      },

    // 青云客机器人
    robot:function () {
        var that = this
        wx.request({
            url: 'http://api.qingyunke.com/api.php',
            data: {
              key: "free",
              "appid": 0,
              "msg": this.data.txt
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: function (res) {
              console.log("青云客：",res.data);
            //   去除{br}
              var arr = [/{br}/g]
              var result = res.data.content;
              for (let i = 0; i < arr.length; i++) {
                result = result.replace(arr[i], '')
              }
              that.setData({
                msg: result
              })
            },
            fail: function (res) {
                console.log("青云客人", res)
              }
          })
    },
    onLoad(options) {
      console.log(options);
      var that = this;
      that.setData({
        title: options.title,
        image: options.image,
        description: options.description,
      })
      console.log(that.data.description);
      if (that.data.description === 'undefined') {
        that.setData({
          image: '../../images/plant.jpg',
          description:'暂时没有相关信息噢~~~'
        })
      }
      console.log('LATER',that.data.description);
  },
  
})